<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Inventory;

use App\Models\Basics\SysArea;
use App\Models\Inventory\Transfer;
use App\Models\Inventory\TransferA;
use App\Models\Inventory\TransferP;
use App\Models\Stocking\StockingSku;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
class TransferAPAjax {

    public function transferModal($id){
        $transferAP = TransferA::query()->find($id);
        return view('inventory.transfer-ap.modal-transfer', compact('id','transferAP'));
    }

    /**
     * 站点库存挪用至PUBLIC库存
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function transfer($param){
        try{
            $id = $param['id'];
            $transferNum = $param['transfer_num'];
            if($transferNum == 0){
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }
            DB::beginTransaction();

            //站点库存
            $transferA = TransferA::query()->find($id);
            $oAreaId = $transferA->area_id;
            $pSkuId = $transferA->psku_id;
            $instant = $transferA->instant;
            if($instant < $transferNum){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '可挪用库存数量不足！']);
            }
            $iAreaId = SysArea::getSysAreaIdByArea();
            if($oAreaId == $iAreaId){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '请勿挪用PUBLIC站点库存！']);
            }
            $transferA->instant -= $transferNum;
            $transferA->area_transit += $transferNum;
            $flag1 = $transferA->save();
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '站点库存扣减失败']);
            }

            //公共库存
            $transferP = TransferP::query()->firstOrNew(['area_id' => $iAreaId,'psku_id' => $pSkuId]);
            $transferP->instant += $transferNum;
            $flag2 = $transferP->save();
            if(!$flag2){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => 'PUBLIC库存添加失败']);
            }
            $userId = Admin::user()->id;

            //挪用记录
            $transfer = Transfer::query()->create([
                'psku_id' => $pSkuId,
                'iuser_id' => $userId,
                'iarea_id' => $iAreaId,
                'ouser_id' => $userId,
                'oarea_id' => $oAreaId,
                'deduct_transit' => 1,
                'transfer_num' => $transferNum,
                'status' => 2,
            ]);
            if($transfer){
                StockingSku::upCnInstant($oAreaId.'_'.$pSkuId);
                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }else{
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统错误']);
        }
    }
}
